{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "localtext = open('6.html', 'r', encoding='utf8')\n",
    "sp = BeautifulSoup(localtext, 'lxml')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<a class=\"current\" href=\"/6.html\">6_html</a>, <a href=\"/5.html\">5_html</a>, <a href=\"/4.html\">4_html</a>, <a href=\"/3.html\">3_html</a>, <a href=\"/2.html\">2_html</a>]\n"
     ]
    }
   ],
   "source": [
    "## 子集操作\n",
    "print(sp.find_all('a'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "div2 = sp.find('div', {'class':'div2'})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<a class=\"current\" href=\"/6.html\">6_html</a>, <a href=\"/5.html\">5_html</a>]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "div2.find_all('a')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<div id=\"divid\">\n",
       "<div class=\"div1 div2\">\n",
       "<a class=\"current\" href=\"/6.html\">6_html</a>\n",
       "<a href=\"/5.html\">5_html</a>\n",
       "<p>p4</p>\n",
       "<p>p5</p>\n",
       "<p>p6</p>\n",
       "</div>\n",
       "<div class=\"div1 div3\">\n",
       "<a href=\"/4.html\">4_html</a>\n",
       "<a href=\"/3.html\">3_html</a>\n",
       "<a href=\"/2.html\">2_html</a>\n",
       "<p>p1</p>\n",
       "<p>p2</p>\n",
       "<p>p3</p>\n",
       "</div>\n",
       "</div>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 查找父节点\n",
    "# div2.find_parent()\n",
    "div2.parent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<div id=\"divid\">\n",
       " <div class=\"div1 div2\">\n",
       " <a class=\"current\" href=\"/6.html\">6_html</a>\n",
       " <a href=\"/5.html\">5_html</a>\n",
       " <p>p4</p>\n",
       " <p>p5</p>\n",
       " <p>p6</p>\n",
       " </div>\n",
       " <div class=\"div1 div3\">\n",
       " <a href=\"/4.html\">4_html</a>\n",
       " <a href=\"/3.html\">3_html</a>\n",
       " <a href=\"/2.html\">2_html</a>\n",
       " <p>p1</p>\n",
       " <p>p2</p>\n",
       " <p>p3</p>\n",
       " </div>\n",
       " </div>, <body>\n",
       " <div id=\"divid\">\n",
       " <div class=\"div1 div2\">\n",
       " <a class=\"current\" href=\"/6.html\">6_html</a>\n",
       " <a href=\"/5.html\">5_html</a>\n",
       " <p>p4</p>\n",
       " <p>p5</p>\n",
       " <p>p6</p>\n",
       " </div>\n",
       " <div class=\"div1 div3\">\n",
       " <a href=\"/4.html\">4_html</a>\n",
       " <a href=\"/3.html\">3_html</a>\n",
       " <a href=\"/2.html\">2_html</a>\n",
       " <p>p1</p>\n",
       " <p>p2</p>\n",
       " <p>p3</p>\n",
       " </div>\n",
       " </div>\n",
       " </body>, <html lang=\"en\">\n",
       " <head>\n",
       " <meta charset=\"utf-8\"/>\n",
       " <meta content=\"width=device-width, initial-scale=1.0\" name=\"viewport\"/>\n",
       " <meta content=\"ie=edge\" http-equiv=\"X-UA-Compatible\"/>\n",
       " <title>标签查找</title>\n",
       " </head>\n",
       " <body>\n",
       " <div id=\"divid\">\n",
       " <div class=\"div1 div2\">\n",
       " <a class=\"current\" href=\"/6.html\">6_html</a>\n",
       " <a href=\"/5.html\">5_html</a>\n",
       " <p>p4</p>\n",
       " <p>p5</p>\n",
       " <p>p6</p>\n",
       " </div>\n",
       " <div class=\"div1 div3\">\n",
       " <a href=\"/4.html\">4_html</a>\n",
       " <a href=\"/3.html\">3_html</a>\n",
       " <a href=\"/2.html\">2_html</a>\n",
       " <p>p1</p>\n",
       " <p>p2</p>\n",
       " <p>p3</p>\n",
       " </div>\n",
       " </div>\n",
       " </body>\n",
       " </html>, <!DOCTYPE html>\n",
       " <html lang=\"en\">\n",
       " <head>\n",
       " <meta charset=\"utf-8\"/>\n",
       " <meta content=\"width=device-width, initial-scale=1.0\" name=\"viewport\"/>\n",
       " <meta content=\"ie=edge\" http-equiv=\"X-UA-Compatible\"/>\n",
       " <title>标签查找</title>\n",
       " </head>\n",
       " <body>\n",
       " <div id=\"divid\">\n",
       " <div class=\"div1 div2\">\n",
       " <a class=\"current\" href=\"/6.html\">6_html</a>\n",
       " <a href=\"/5.html\">5_html</a>\n",
       " <p>p4</p>\n",
       " <p>p5</p>\n",
       " <p>p6</p>\n",
       " </div>\n",
       " <div class=\"div1 div3\">\n",
       " <a href=\"/4.html\">4_html</a>\n",
       " <a href=\"/3.html\">3_html</a>\n",
       " <a href=\"/2.html\">2_html</a>\n",
       " <p>p1</p>\n",
       " <p>p2</p>\n",
       " <p>p3</p>\n",
       " </div>\n",
       " </div>\n",
       " </body>\n",
       " </html>]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 查找全部的父节点\n",
    "# list(div2.parents)\n",
    "div2.find_parents()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<body>\n",
       "<div id=\"divid\">\n",
       "<div class=\"div1 div2\">\n",
       "<a class=\"current\" href=\"/6.html\">6_html</a>\n",
       "<a href=\"/5.html\">5_html</a>\n",
       "<p>p4</p>\n",
       "<p>p5</p>\n",
       "<p>p6</p>\n",
       "</div>\n",
       "<div class=\"div1 div3\">\n",
       "<a href=\"/4.html\">4_html</a>\n",
       "<a href=\"/3.html\">3_html</a>\n",
       "<a href=\"/2.html\">2_html</a>\n",
       "<p>p1</p>\n",
       "<p>p2</p>\n",
       "<p>p3</p>\n",
       "</div>\n",
       "</div>\n",
       "</body>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 指定标签名的父节点\n",
    "div2.find_parent('body')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "div2 = sp.find('div', {'class':'div2'})\n",
    "\n",
    "## 本节点和后续节点包含的p标签\n",
    "print(div2.find_all_next('p'))\n",
    "## \n",
    "print(div2.find_all_previous('p'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 第二个p标签\n",
    "p5 = div2.find_all('p')[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<p>p4</p>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p5.find_previous('a')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<p>p4</p>, <a href=\"/5.html\">5_html</a>, <a class=\"current\" href=\"/6.html\">6_html</a>, <div class=\"div1 div2\">\n",
      "<a class=\"current\" href=\"/6.html\">6_html</a>\n",
      "<a href=\"/5.html\">5_html</a>\n",
      "<p>p4</p>\n",
      "<p>p5</p>\n",
      "<p>p6</p>\n",
      "</div>, <div id=\"divid\">\n",
      "<div class=\"div1 div2\">\n",
      "<a class=\"current\" href=\"/6.html\">6_html</a>\n",
      "<a href=\"/5.html\">5_html</a>\n",
      "<p>p4</p>\n",
      "<p>p5</p>\n",
      "<p>p6</p>\n",
      "</div>\n",
      "<div class=\"div1 div3\">\n",
      "<a href=\"/4.html\">4_html</a>\n",
      "<a href=\"/3.html\">3_html</a>\n",
      "<a href=\"/2.html\">2_html</a>\n",
      "<p>p1</p>\n",
      "<p>p2</p>\n",
      "<p>p3</p>\n",
      "</div>\n",
      "</div>, <body>\n",
      "<div id=\"divid\">\n",
      "<div class=\"div1 div2\">\n",
      "<a class=\"current\" href=\"/6.html\">6_html</a>\n",
      "<a href=\"/5.html\">5_html</a>\n",
      "<p>p4</p>\n",
      "<p>p5</p>\n",
      "<p>p6</p>\n",
      "</div>\n",
      "<div class=\"div1 div3\">\n",
      "<a href=\"/4.html\">4_html</a>\n",
      "<a href=\"/3.html\">3_html</a>\n",
      "<a href=\"/2.html\">2_html</a>\n",
      "<p>p1</p>\n",
      "<p>p2</p>\n",
      "<p>p3</p>\n",
      "</div>\n",
      "</div>\n",
      "</body>, <title>标签查找</title>, <meta content=\"ie=edge\" http-equiv=\"X-UA-Compatible\"/>, <meta content=\"width=device-width, initial-scale=1.0\" name=\"viewport\"/>, <meta charset=\"utf-8\"/>, <head>\n",
      "<meta charset=\"utf-8\"/>\n",
      "<meta content=\"width=device-width, initial-scale=1.0\" name=\"viewport\"/>\n",
      "<meta content=\"ie=edge\" http-equiv=\"X-UA-Compatible\"/>\n",
      "<title>标签查找</title>\n",
      "</head>, <html lang=\"en\">\n",
      "<head>\n",
      "<meta charset=\"utf-8\"/>\n",
      "<meta content=\"width=device-width, initial-scale=1.0\" name=\"viewport\"/>\n",
      "<meta content=\"ie=edge\" http-equiv=\"X-UA-Compatible\"/>\n",
      "<title>标签查找</title>\n",
      "</head>\n",
      "<body>\n",
      "<div id=\"divid\">\n",
      "<div class=\"div1 div2\">\n",
      "<a class=\"current\" href=\"/6.html\">6_html</a>\n",
      "<a href=\"/5.html\">5_html</a>\n",
      "<p>p4</p>\n",
      "<p>p5</p>\n",
      "<p>p6</p>\n",
      "</div>\n",
      "<div class=\"div1 div3\">\n",
      "<a href=\"/4.html\">4_html</a>\n",
      "<a href=\"/3.html\">3_html</a>\n",
      "<a href=\"/2.html\">2_html</a>\n",
      "<p>p1</p>\n",
      "<p>p2</p>\n",
      "<p>p3</p>\n",
      "</div>\n",
      "</div>\n",
      "</body>\n",
      "</html>]\n",
      "-------------------------\n",
      "[<p>p6</p>, <div class=\"div1 div3\">\n",
      "<a href=\"/4.html\">4_html</a>\n",
      "<a href=\"/3.html\">3_html</a>\n",
      "<a href=\"/2.html\">2_html</a>\n",
      "<p>p1</p>\n",
      "<p>p2</p>\n",
      "<p>p3</p>\n",
      "</div>, <a href=\"/4.html\">4_html</a>, <a href=\"/3.html\">3_html</a>, <a href=\"/2.html\">2_html</a>, <p>p1</p>, <p>p2</p>, <p>p3</p>]\n"
     ]
    }
   ],
   "source": [
    "print(p5.find_all_previous())\n",
    "print('-------------------------')\n",
    "print(p5.find_all_next())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<p>p6</p>]\n",
      "-----------------------\n",
      "[<p>p4</p>, <a href=\"/5.html\">5_html</a>, <a class=\"current\" href=\"/6.html\">6_html</a>]\n"
     ]
    }
   ],
   "source": [
    "## 同级操作\n",
    "print(p5.find_next_siblings())\n",
    "print('-----------------------')\n",
    "print(p5.find_previous_siblings())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
